package com.example.demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.PmEmployee;
import com.example.demo.mapper.PmEmployeeMapper;
import com.example.demo.service.PmEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PmEmployeeServiceImpl extends ServiceImpl<PmEmployeeMapper, PmEmployee> implements PmEmployeeService {
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private PmEmployeeMapper pmEmployeeMapper;

    //人员结构表
    @Override
    public Map<String, Object> getMap1(String startDate, String endDate, String company) {
        Map<String, Object> map = new HashMap<>();

        LambdaQueryWrapper<PmEmployee> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PmEmployee::getIsLeave, false)
                .eq(PmEmployee::getIsDeleted, false)
                .le(PmEmployee::getOnboardingTime, endDate);
        if (!company.equals("%")) wrapper.eq(PmEmployee::getCompany, company);
        int total = pmEmployeeMapper.selectCount(wrapper);
        int regular_total = pmEmployeeMapper.selectCount(wrapper.eq(PmEmployee::getIsRegular, true));
        map.put("start_date", startDate);
        map.put("end_date", endDate);

        //学历状况
        Long master = 0L, bachelor = 0L, college = 0L, below = 0L;
        List<HashMap<String, Long>> list1 = pmEmployeeMapper.countByDegree(endDate, company);
        if(list1.size() > 0){
            for(HashMap<String, Long> item : list1){
                String type = String.valueOf(item.get("degree"));
                if(type.contains("硕士")){
                    master += item.get("count");
                }else if(type.contains("本科")){
                    bachelor += item.get("count");
                }else if(type.contains("大专")){
                    college += item.get("count");
                }else{
                    below += item.get("count");
                }
            }
        }
        map.put("c_master", master);
        map.put("c_bachelor", bachelor);
        map.put("c_college", college);
        map.put("c_below", below);
        map.put("regular_total", regular_total);

        //岗序状况
        Long leader = 0L, manager = 0L, ordinary = 0L, other = 0L;
        List<HashMap<String, Long>> list2 = pmEmployeeMapper.countByPositionStatus(endDate, company);
        if(list2.size() > 0){
            for(HashMap<String, Long> item : list2){
                String type = String.valueOf(item.get("position_status"));
                if(type.contains("经营层")){
                    leader += item.get("count");
                }else if(type.contains("中层")){
                    manager += item.get("count");
                }else if(type.contains("普通员工")){
                    ordinary += item.get("count");
                }else{
                    other += item.get("count");
                }
            }
        }
        map.put("leader", leader);
        map.put("manager", manager);
        map.put("ordinary", ordinary);
        map.put("other", other);
        map.put("total", total);


        //年龄状况
        Long age20 = 0L, age30 = 0L, age40 = 0L, age50 = 0L;
        List<HashMap<String, Long>> list3 = pmEmployeeMapper.countByAge(endDate, company);
        if(list3.size() > 0){
            for(HashMap<String, Long> item : list3){
                String type = String.valueOf(item.get("age_temp"));
                if(type.contains("30-")){
                    age20 += item.get("count");
                }else if(type.contains("31-40")){
                    age30 += item.get("count");
                }else if(type.contains("41-50")){
                    age40 += item.get("count");
                }else{
                    age50 += item.get("count");
                }
            }
        }
        double average_age = 0;
        double age_sum = pmEmployeeMapper.ageSum(endDate, company).get("age");
        if (age_sum > 0 && regular_total > 0){
            average_age = pmEmployeeMapper.ageSum(endDate, company).get("age") / regular_total;
            BigDecimal average_age_temp = new BigDecimal(average_age);
            average_age = average_age_temp.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        map.put("c_age20", age20);
        map.put("c_age30", age30);
        map.put("c_age40", age40);
        map.put("c_age50", age50);
        map.put("average_age", average_age);

        //人员情况
        Long temp = 0L, intern = 0L, retired = 0L;
        List<HashMap<String, Long>> list4 = pmEmployeeMapper.countByEmployeeType(endDate, company);
        if (list4.size() > 0){
            for(HashMap<String, Long> item : list4){
                String type = String.valueOf(item.get("employment_type"));
                if(type.contains("外包") || type.contains("兼职") || type.contains("临时工")){
                    temp += item.get("count");
                }else if(type.contains("实习生")){
                    intern += item.get("count");
                }else if(type.contains("退休返聘")){
                    retired += item.get("count");
                }
            }
        }
        map.put("c_temp", temp);
        map.put("c_intern", intern);
        map.put("c_retired", retired);

        //政治面貌
        Long cpc_member = 0L, probationary_party_member = 0L, league_member = 0L;
        List<HashMap<String, Long>> list5 = pmEmployeeMapper.countByPoliticalStatus(endDate, company);
        if (list5.size() > 0){
            for(HashMap<String, Long> item : list5){
                String type = String.valueOf(item.get("political_status"));
                if(type.contains("中共党员")){
                    cpc_member += item.get("count");
                }else if(type.contains("预备党员")){
                    probationary_party_member += item.get("count");
                }else if(type.contains("团员")){
                    league_member += item.get("count");
                }
            }
        }
        map.put("cpc_member", cpc_member);
        map.put("pro_member", probationary_party_member);
        map.put("league_member", league_member);

        //本月新增人数
        Long add_leader = 0L, add_manager = 0L, add_ordinary = 0L, add_other = 0L;
        List<HashMap<String, Long>> list6 = pmEmployeeMapper.newThisMonthPositionStatus(startDate, endDate, company);
        if (list6.size() > 0){
            for(HashMap<String, Long> item : list6){
                String type = String.valueOf(item.get("position_status"));
                if(type.contains("经营层")){
                    add_leader += item.get("count");
                }else if(type.contains("中层")){
                    add_manager += item.get("count");
                }else if(type.contains("普通员工")){
                    add_ordinary += item.get("count");
                }else{
                    add_other += item.get("count");
                }
            }
        }
        map.put("add_leader", add_leader);
        map.put("add_manager", add_manager);
        map.put("add_ordinary", add_ordinary);
        map.put("add_other", add_other);
        map.put("add_total", pmEmployeeMapper.newSum(startDate, endDate, company).get("count"));

        //本月离职人数
        Long leave_leader = 0L, leave_manager = 0L, leave_ordinary = 0L, leave_other = 0L;
        List<HashMap<String, Long>> list7 = pmEmployeeMapper.leaveThisMonthPositionStatus(startDate, endDate, company);
        if (list7.size() > 0){
            for(HashMap<String, Long> item : list7){
                String type = String.valueOf(item.get("position_status"));
                if(type.contains("经营层")){
                    leave_leader += item.get("count");
                }else if(type.contains("中层管理")){
                    leave_manager += item.get("count");
                }else if(type.contains("普通员工")){
                    leave_ordinary += item.get("count");
                }else{
                    leave_other += item.get("count");
                }
            }
        }
        map.put("leave_leader", leave_leader);
        map.put("leave_manager", leave_manager);
        map.put("leave_ordinary", leave_ordinary);
        map.put("leave_other", leave_other);
        map.put("leave_total", leave_leader + leave_manager + leave_ordinary + leave_other);

        //女职工数据
        Long female = 0L;
        double female_proportion = 0;
        List<HashMap<String, Long>> list8 = pmEmployeeMapper.countBySex(endDate, company);
        if (list8.size() > 0){
            for(HashMap<String, Long> item : list8){
                String type = String.valueOf(item.get("sex"));
                if(type.equals("女")){
                    female += item.get("count");
                }
            }
        }
        double regular = regular_total;
        if (female > 0 && regular > 0){
            female_proportion = female / regular;
        }
        NumberFormat fmt = NumberFormat.getPercentInstance();
        fmt.setMaximumFractionDigits(2);
        map.put("female", female);
        map.put("female_proportion", fmt.format(female_proportion));

        return map;
    }

    //人员流动表
    @Override
    public Map<String, Object> getMap2(String startDate, String endDate, String company) {
//        String startDate = getMinMonthDate(date);
//        String endDate = getMaxMonthDate(date);
        Map<String, Object> map = new HashMap<>();

        LambdaQueryWrapper<PmEmployee> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(PmEmployee::getIsLeave, false)
                .eq(PmEmployee::getIsDeleted, false)
                .eq(PmEmployee::getIsRegular, true)
                .le(PmEmployee::getOnboardingTime, endDate);
        if (!company.equals("%")) wrapper1.eq(PmEmployee::getCompany, company);
        int regular_total = pmEmployeeMapper.selectCount(wrapper1);
        map.put("regular_total", regular_total);
        map.put("start_date", startDate);
        map.put("end_date", endDate);

        //本月入职员工来源
        Long fresh_graduate = 0L, social = 0L, cross_class = 0L, platform = 0L, external = 0L;
        List<HashMap<String, Long>> list1 = pmEmployeeMapper.newThisMonthRecruitmentSource(startDate, endDate, company);
        if (list1.size() > 0){
            for(HashMap<String, Long> item : list1){
                String type = String.valueOf(item.get("recruitment_source"));
                if(type.contains("应届生")){
                    fresh_graduate += item.get("count");
                }else if (type.contains("社会招聘")){
                    social += item.get("count");
                }else if (type.contains("集团内部跨一级")){
                    cross_class += item.get("count");
                }else if (type.contains("平台公司")){
                    platform += item.get("count");
                }else if (type.contains("集团外部调入")){
                    external += item.get("count");
                }
            }
        }
        map.put("fresh", fresh_graduate);
        map.put("social", social);
        map.put("cross", cross_class);
        map.put("platform", platform);
        map.put("external", external);

        //本月入职岗位分布
        Long add_leader_principal = 0L, add_leader = 0L, add_manager_principal = 0L, add_manager = 0L, add_ordinary = 0L;
        List<HashMap<String, Long>> list2 = pmEmployeeMapper.newThisMonthPositionStatus(startDate, endDate, company);
        if (list2.size() > 0){
            for(HashMap<String, Long> item : list2){
                String type = String.valueOf(item.get("position_status"));
                if(type.contains("经营层正职")){
                    add_leader_principal += item.get("count");
                }else if(type.contains("经营层副职")){
                    add_leader += item.get("count");
                }else if(type.contains("中层正职")){
                    add_manager_principal += item.get("count");
                }else if (type.contains("中层副职")){
                    add_manager += item.get("count");
                }else if (type.contains("普通员工")){
                    add_ordinary += item.get("count");
                }
            }
        }
        map.put("add_leader_principal", add_leader_principal);
        map.put("add_leader", add_leader);
        map.put("add_manager_principal", add_manager_principal);
        map.put("add_manager", add_manager);
        map.put("add_ordinary", add_ordinary);
        Long add_total = pmEmployeeMapper.newSumRegular(startDate, endDate, company).get("count");
        map.put("add_total", add_total);
        map.put("add_female", pmEmployeeMapper.newSumFemale(startDate, endDate, company).get("count"));

        //人员减少原因
        Long personal = 0L, dismiss = 0L, transfer = 0L, retire = 0L, other_reason = 0L;
        List<HashMap<String, Long>> list3 = pmEmployeeMapper.leaveThisMonthReason(startDate, endDate, company);
        if (list3.size() > 0){
            for(HashMap<String, Long> item : list3){
                String type = String.valueOf(item.get("leave_reason"));
                if(type.contains("个人辞职")){
                    personal += item.get("count");
                }else if(type.contains("公司辞退")){
                    dismiss += item.get("count");
                }else if(type.contains("组织调出")){
                    transfer += item.get("count");
                }else if (type.contains("退休")){
                    retire += item.get("count");
                }else {
                    other_reason += item.get("count");
                }
            }
        }
        map.put("personal", personal);
        map.put("dismiss", dismiss);
        map.put("transfer", transfer);
        map.put("retire", retire);
        map.put("other_reason", other_reason);
        Long leave_total = pmEmployeeMapper.leaveSum(startDate, endDate, company).get("count");
        map.put("leave_total", leave_total);
        map.put("leave_female", pmEmployeeMapper.leaveSumFemale(startDate, endDate, company).get("count"));
        Long regular_total1 = (long) regular_total;
        Long last_regular_total = regular_total1 + leave_total - add_total;
        map.put("last_regular_total", last_regular_total);

        //离职岗位分布
        Long leave_leader_principal = 0L, leave_leader = 0L, leave_manager_principal = 0L, leave_manager = 0L, leave_ordinary = 0L;
        List<HashMap<String, Long>> list4 = pmEmployeeMapper.leaveThisMonthPositionStatus(startDate, endDate, company);
        if (list4.size() > 0){
            for(HashMap<String, Long> item : list4){
                String type = String.valueOf(item.get("position_status"));
                if(type.contains("经营层正职")){
                    leave_leader_principal += item.get("count");
                }else if(type.contains("经营层副职")){
                    leave_leader += item.get("count");
                }else if(type.contains("中层正职")){
                    leave_manager_principal += item.get("count");
                }else if (type.contains("中层副职")){
                    leave_manager += item.get("count");
                }else if (type.contains("普通员工")){
                    leave_ordinary += item.get("count");
                }
            }
        }
        map.put("leave_leader_principal", leave_leader_principal);
        map.put("leave_leader", leave_leader);
        map.put("leave_manager_principal", leave_manager_principal);
        map.put("leave_manager", leave_manager);
        map.put("leave_ordinary", leave_ordinary);

        //特殊用工新增
        Long add_intern = 0L, add_rehire = 0L, add_outsourcing = 0L, add_other_type = 0L;
        List<HashMap<String, Long>> list5 = pmEmployeeMapper.newThisMonthEmploymentType(startDate, endDate, company);
        if (list5.size() > 0){
            for(HashMap<String, Long> item : list5){
                String type = String.valueOf(item.get("employment_type"));
                if(type.contains("实习")){
                    add_intern += item.get("count");
                }else if (type.contains("退休返聘")){
                    add_rehire += item.get("count");
                }else if (type.contains("劳务") || type.contains("外包")){
                    add_outsourcing += item.get("count");
                }else if (type.contains("兼职") || type.contains("挂职")){
                    add_other_type += item.get("count");
                }
            }
        }
        map.put("add_intern", add_intern);
        map.put("add_rehire", add_rehire);
        map.put("add_outsourcing", add_outsourcing);
        map.put("add_other_type", add_other_type);

        //特殊用工减少
        Long leave_intern = 0L, leave_rehire = 0L, leave_outsourcing = 0L, leave_other_type = 0L;
        List<HashMap<String, Long>> list6 = pmEmployeeMapper.leaveThisMonthEmploymentType(startDate, endDate, company);
        if (list6.size() > 0){
            for(HashMap<String, Long> item : list6){
                String type = String.valueOf(item.get("employment_type"));
                if (type.contains("实习")){
                    leave_intern += item.get("count");
                }else if (type.contains("退休返聘")){
                    leave_rehire += item.get("count");
                }else if (type.contains("外包")){
                    leave_outsourcing += item.get("count");
                }else if (type.contains("兼职") || type.contains("挂职")){
                    leave_other_type += item.get("count");
                }
            }
        }
        map.put("leave_intern", leave_intern);
        map.put("leave_rehire", leave_rehire);
        map.put("leave_outsourcing", leave_outsourcing);
        map.put("leave_other_type", leave_other_type);

        //特殊用工数量
        Long intern = 0L, rehire = 0L, outsourcing = 0L, other_type = 0L;
        List<HashMap<String, Long>> list7 = pmEmployeeMapper.countByEmployeeType(endDate, company);
        if (list7.size() > 0){
            for(HashMap<String, Long> item : list7){
                String type = String.valueOf(item.get("employment_type"));
                if(type.contains("实习")){
                    intern += item.get("count");
                }else if(type.contains("退休返聘")){
                    rehire += item.get("count");
                }else if(type.contains("外包")){
                    outsourcing += item.get("count");
                }else if (type.contains("兼职") || type.contains("挂职")){
                    other_type += item.get("count");
                }
            }
        }
        Long not_regular_total = intern + rehire + outsourcing + other_type;
        map.put("intern", intern);
        map.put("rehire", rehire);
        map.put("outsourcing", outsourcing);
        map.put("other_type", other_type);
        map.put("not_regular_total", not_regular_total);

        return map;
    }

    //新进员工表
    @Override
    public Map<String, Object> getMap3(String startDate, String endDate, String company) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<PmEmployee> wrapper = new LambdaQueryWrapper<>();
        wrapper.le(PmEmployee::getOnboardingTime, endDate)
                .eq(PmEmployee::getIsDeleted, false)
                .ge(PmEmployee::getOnboardingTime, startDate);
        if (!company.equals("%")) wrapper.eq(PmEmployee::getCompany, company);
        List<PmEmployee> list = pmEmployeeMapper.selectList(wrapper);
        for (int i=0; i < list.size(); i++){
            list.get(i).setId((long) i + 1);
        }
        map.put("list", list);
        map.put("start_date", startDate);
        map.put("end_date", endDate);
        return map;
    }

    //下属公司人员信息表
    @Override
    public Map<String, Object> getMap4(String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        String[] company = {"宁波市会展博览集团有限公司", "宁波国际会议中心管理有限公司", "宁波国际会议中心酒店有限公司",
                "宁波钱湖酒店有限公司东钱湖康得思酒店", "宁波文化广场朗豪酒店有限公司", "宁波钱湖国宾馆管理有限公司", "宁波新芝宾馆经营有限公司"};
        List<HashMap<String, Long>> list;
        LambdaQueryWrapper<PmEmployee> wrapper = Wrappers.lambdaQuery();
        wrapper.le(PmEmployee::getOnboardingTime, endDate)
                .eq(PmEmployee::getIsDeleted, false)
                .eq(PmEmployee::getIsLeave, false);
        int all = pmEmployeeMapper.selectCount(wrapper);
        int regular_total = pmEmployeeMapper.selectCount(wrapper.eq(PmEmployee::getIsRegular, true));
        map.put("all", all);
        map.put("total", regular_total);

        //百分数转化
        NumberFormat fmt = NumberFormat.getPercentInstance();
        fmt.setMaximumFractionDigits(2);
        double regular_total1 = regular_total;

        //学历统计
        Long master_total = 0L, bachelor_total = 0L, college_total = 0L, secondary_total = 0L, below_total = 0L;
        for(String str : company){
            list = pmEmployeeMapper.countByDegree(endDate, str);
            Long master = 0L, bachelor = 0L, college = 0L, secondary = 0L, below = 0L;
            for(HashMap<String, Long> item : list){
                String type = String.valueOf(item.get("degree"));
                if (type.contains("硕士") || type.contains("博士")){
                    master += item.get("count");
                } else if (type.contains("本科")){
                    bachelor += item.get("count");
                } else if (type.contains("大专")){
                    college += item.get("count");
                } else if (type.contains("中专")){
                    secondary += item.get("count");
                } else
                    below += item.get("count");
            }
            Long total = master + bachelor + college + secondary + below;
            double total1 = total;
            if (str.equals("宁波市会展博览集团有限公司")){
                map.put("hz_master", master);
                map.put("hz_bachelor", bachelor);
                map.put("hz_college", college);
                map.put("hz_secondary", secondary);
                map.put("hz_below", below);
                map.put("hz_total", total);
                map.put("hz_above_bachelor", master + bachelor);
                map.put("hz_below_secondary", secondary + below);
                if (total == 0) {
                    map.put("hz_bachelor_rate", 0);
                    map.put("hz_college_rate", 0);
                    map.put("hz_below_secondary_rate", 0);
                }else {
                    map.put("hz_bachelor_rate", fmt.format((master + bachelor) / total1));
                    map.put("hz_college_rate", fmt.format(college / total1));
                    map.put("hz_below_secondary_rate", fmt.format((secondary + below) / total1));
                }
            } else if (str.equals("宁波国际会议中心管理有限公司")){
                map.put("gh_master", master);
                map.put("gh_bachelor", bachelor);
                map.put("gh_college", college);
                map.put("gh_secondary", secondary);
                map.put("gh_below", below);
                map.put("gh_total", total);
                map.put("gh_above_bachelor", master + bachelor);
                map.put("gh_below_secondary", secondary + below);
                if (total == 0) {
                    map.put("gh_bachelor_rate", 0);
                    map.put("gh_college_rate", 0);
                    map.put("gh_below_secondary_rate", 0);
                }else {
                    map.put("gh_bachelor_rate", fmt.format((master + bachelor) / total1));
                    map.put("gh_college_rate", fmt.format(college / total1));
                    map.put("gh_below_secondary_rate", fmt.format((secondary + below) / total1));
                }
            } else if (str.equals("宁波国际会议中心酒店有限公司")){
                map.put("ghh_master", master);
                map.put("ghh_bachelor", bachelor);
                map.put("ghh_college", college);
                map.put("ghh_secondary", secondary);
                map.put("ghh_below", below);
                map.put("ghh_total", total);
                map.put("ghh_above_bachelor", master + bachelor);
                map.put("ghh_below_secondary", secondary + below);
                if (total == 0) {
                    map.put("ghh_bachelor_rate", 0);
                    map.put("ghh_college_rate", 0);
                    map.put("ghh_below_secondary_rate", 0);
                }else {
                    map.put("ghh_bachelor_rate", fmt.format((master + bachelor) / total1));
                    map.put("ghh_college_rate", fmt.format(college / total1));
                    map.put("ghh_below_secondary_rate", fmt.format((secondary + below) / total1));
                }
            } else if (str.equals("宁波钱湖酒店有限公司东钱湖康得思酒店")){
                map.put("kd_master", master);
                map.put("kd_bachelor", bachelor);
                map.put("kd_college", college);
                map.put("kd_secondary", secondary);
                map.put("kd_below", below);
                map.put("kd_total", total);
                map.put("kd_above_bachelor", master + bachelor);
                map.put("kd_below_secondary", secondary + below);
                if (total == 0) {
                    map.put("kd_bachelor_rate", 0);
                    map.put("kd_college_rate", 0);
                    map.put("kd_below_secondary_rate", 0);
                }else {
                    map.put("kd_bachelor_rate", fmt.format((master + bachelor) / total1));
                    map.put("kd_college_rate", fmt.format(college / total1));
                    map.put("kd_below_secondary_rate", fmt.format((secondary + below) / total1));
                }
            } else if (str.equals("宁波文化广场朗豪酒店有限公司")){
                map.put("lh_master", master);
                map.put("lh_bachelor", bachelor);
                map.put("lh_college", college);
                map.put("lh_secondary", secondary);
                map.put("lh_below", below);
                map.put("lh_total", total);
                map.put("lh_above_bachelor", master + bachelor);
                map.put("lh_below_secondary", secondary + below);
                if (total == 0) {
                    map.put("lh_bachelor_rate", 0);
                    map.put("lh_college_rate", 0);
                    map.put("lh_below_secondary_rate", 0);
                }else {
                    map.put("lh_bachelor_rate", fmt.format((master + bachelor) / total1));
                    map.put("lh_college_rate", fmt.format(college / total1));
                    map.put("lh_below_secondary_rate", fmt.format((secondary + below) / total1));
                }
            } else if (str.equals("宁波钱湖国宾馆管理有限公司")){
                map.put("qian_master", master);
                map.put("qian_bachelor", bachelor);
                map.put("qian_college", college);
                map.put("qian_secondary", secondary);
                map.put("qian_below", below);
                map.put("qian_total", total);
                map.put("qian_above_bachelor", master + bachelor);
                map.put("qian_below_secondary", secondary + below);
                if (total == 0) {
                    map.put("qian_bachelor_rate", 0);
                    map.put("qian_college_rate", 0);
                    map.put("qian_below_secondary_rate", 0);
                }else {
                    map.put("qian_bachelor_rate", fmt.format((master + bachelor) / total1));
                    map.put("qian_college_rate", fmt.format(college / total1));
                    map.put("qian_below_secondary_rate", fmt.format((secondary + below) / total1));
                }
            } else {
                map.put("xin_master", master);
                map.put("xin_bachelor", bachelor);
                map.put("xin_college", college);
                map.put("xin_secondary", secondary);
                map.put("xin_below", below);
                map.put("xin_total", total);
                map.put("xin_above_bachelor", master + bachelor);
                map.put("xin_below_secondary", secondary + below);
                if (total == 0) {
                    map.put("xin_bachelor_rate", 0);
                    map.put("xin_college_rate", 0);
                    map.put("xin_below_secondary_rate", 0);
                }else {
                    map.put("xin_bachelor_rate", fmt.format((master + bachelor) / total1));
                    map.put("xin_college_rate", fmt.format(college / total1));
                    map.put("xin_below_secondary_rate", fmt.format((secondary + below) / total1));
                }
            }
            master_total += master;
            bachelor_total += bachelor;
            college_total += college;
            secondary_total += secondary;
            below_total += below;
        }
        map.put("master_total", master_total);
        map.put("bachelor_total", bachelor_total);
        map.put("college_total", college_total);
        map.put("secondary_total", secondary_total);
        map.put("below_total", below_total);
        map.put("total_above_bachelor", master_total + bachelor_total);
        map.put("total_bachelor_rate", fmt.format((master_total + bachelor_total) / regular_total1));
        map.put("total_college_rate", fmt.format(college_total / regular_total1));
        map.put("total_below_secondary", secondary_total + below_total);
        map.put("total_below_secondary_rate", fmt.format((secondary_total + below_total) / regular_total1));


        //年龄统计
        Long under30_total = 0L, under40_total = 0L, under50_total = 0L, above51_total = 0L;
        for(String str : company){
            list = pmEmployeeMapper.countByAge(endDate, str);
            Long under30 = 0L, under40 = 0L, under50 = 0L, above51 = 0L;
            for(HashMap<String, Long> item : list){
                String type = String.valueOf(item.get("age_temp"));
                if (type.contains("30-")){
                    under30 += item.get("count");
                } else if (type.contains("31-40")){
                    under40 += item.get("count");
                } else if (type.contains("41-50")){
                    under50 += item.get("count");
                } else if (type.contains("51+")){
                    above51 += item.get("count");
                }
            }
            Long total = under30 + under40 + under50 + above51;
            if (str.equals("宁波市会展博览集团有限公司")){
                map.put("hz_under30", under30);
                map.put("hz_under40", under40);
                map.put("hz_under50", under50);
                map.put("hz_above51", above51);
                map.put("hz_aaa", under40 + under50);
            } else if (str.equals("宁波国际会议中心管理有限公司")){
                map.put("gh_under30", under30);
                map.put("gh_under40", under40);
                map.put("gh_under50", under50);
                map.put("gh_above51", above51);
                map.put("gh_aaa", under40 + under50);
            } else if (str.equals("宁波国际会议中心酒店有限公司")){
                map.put("ghh_under30", under30);
                map.put("ghh_under40", under40);
                map.put("ghh_under50", under50);
                map.put("ghh_above51", above51);
                map.put("ghh_aaa", under40 + under50);
            } else if (str.equals("宁波钱湖酒店有限公司东钱湖康得思酒店")){
                map.put("kd_under30", under30);
                map.put("kd_under40", under40);
                map.put("kd_under50", under50);
                map.put("kd_above51", above51);
                map.put("kd_aaa", under40 + under50);
            } else if (str.equals("宁波文化广场朗豪酒店有限公司")){
                map.put("lh_under30", under30);
                map.put("lh_under40", under40);
                map.put("lh_under50", under50);
                map.put("lh_above51", above51);
                map.put("lh_aaa", under40 + under50);
            } else if (str.equals("宁波钱湖国宾馆管理有限公司")){
                map.put("qian_under30", under30);
                map.put("qian_under40", under40);
                map.put("qian_under50", under50);
                map.put("qian_above51", above51);
                map.put("qian_aaa", under40 + under50);
            } else {
                map.put("xin_under30", under30);
                map.put("xin_under40", under40);
                map.put("xin_under50", under50);
                map.put("xin_above51", above51);
                map.put("xin_aaa", under40 + under50);
            }
            under30_total += under30;
            under40_total += under40;
            under50_total += under50;
            above51_total += above51;
        }
        map.put("under30_total", under30_total);
        map.put("under40_total", under40_total);
        map.put("under50_total", under50_total);
        map.put("above51_total", above51_total);
        map.put("total_aaa", under40_total + under50_total);
        map.put("under30_rate", fmt.format(under30_total / regular_total1));
        map.put("under40_rate", fmt.format(under40_total / regular_total1));
        map.put("under50_rate", fmt.format(under50_total / regular_total1));
        map.put("above51_rate", fmt.format(above51_total / regular_total1));


        //职称统计
        Long advanced_total = 0L, middle_total = 0L, primary_total = 0L, title_total = 0L;
        for(String str : company){
            list = pmEmployeeMapper.countByProfessionalTitle(endDate, str);
            Long advanced = 0L, middle = 0L, primary = 0L, title = 0L;
            for(HashMap<String, Long> item : list){
                String type = String.valueOf(item.get("professional_title"));
                if (type.contains("高级")){
                    advanced += item.get("count");
                } else if (type.contains("中级")){
                    middle += item.get("count");
                } else if (type.contains("初级")){
                    primary += item.get("count");
                }
            }
            Long total = advanced + middle + primary;
            if (str.equals("宁波市会展博览集团有限公司")){
                map.put("hz_advanced", advanced);
                map.put("hz_middle", middle);
                map.put("hz_primary", primary);
                map.put("hz_title_total", total);
            } else if (str.equals("宁波国际会议中心管理有限公司")){
                map.put("gh_advanced", advanced);
                map.put("gh_middle", middle);
                map.put("gh_primary", primary);
                map.put("gh_title_total", total);
            } else if (str.equals("宁波国际会议中心酒店有限公司")){
                map.put("ghh_advanced", advanced);
                map.put("ghh_middle", middle);
                map.put("ghh_primary", primary);
                map.put("ghh_title_total", total);
            } else if (str.equals("宁波钱湖酒店有限公司东钱湖康得思酒店")){
                map.put("kd_advanced", advanced);
                map.put("kd_middle", middle);
                map.put("kd_primary", primary);
                map.put("kd_title_total", total);
            } else if (str.equals("宁波文化广场朗豪酒店有限公司")){
                map.put("lh_advanced", advanced);
                map.put("lh_middle", middle);
                map.put("lh_primary", primary);
                map.put("lh_title_total", total);
            } else if (str.equals("宁波钱湖国宾馆管理有限公司")){
                map.put("qian_advanced", advanced);
                map.put("qian_middle", middle);
                map.put("qian_primary", primary);
                map.put("qian_title_total", total);
            } else {
                map.put("xin_advanced", advanced);
                map.put("xin_middle", middle);
                map.put("xin_primary", primary);
                map.put("xin_title_total", total);
            }
            advanced_total += advanced;
            middle_total += middle;
            primary_total += primary;
            title_total += total;
        }
        map.put("advanced_total", advanced_total);
        map.put("middle_total", middle_total);
        map.put("primary_total", primary_total);
        map.put("title_total", title_total);

        //性别统计
        Long male_total = 0L, female_total = 0L;
        for(String str : company){
            list = pmEmployeeMapper.countBySex(endDate, str);
            Long male = 0L, female = 0L;
            for(HashMap<String, Long> item : list){
                String type = String.valueOf(item.get("sex"));
                if (type.contains("男")){
                    male += item.get("count");
                } else if (type.contains("女")){
                    female += item.get("count");
                } 
            }
            if (str.equals("宁波市会展博览集团有限公司")){
                map.put("hz_male", male);
                map.put("hz_female", female);
            } else if (str.equals("宁波国际会议中心管理有限公司")){
                map.put("gh_male", male);
                map.put("gh_female", female);
            } else if (str.equals("宁波国际会议中心酒店有限公司")){
                map.put("ghh_male", male);
                map.put("ghh_female", female);
            } else if (str.equals("宁波钱湖酒店有限公司东钱湖康得思酒店")){
                map.put("kd_male", male);
                map.put("kd_female", female);
            } else if (str.equals("宁波文化广场朗豪酒店有限公司")){
                map.put("lh_male", male);
                map.put("lh_female", female);
            } else if (str.equals("宁波钱湖国宾馆管理有限公司")){
                map.put("qian_male", male);
                map.put("qian_female", female);
            } else {
                map.put("xin_male", male);
                map.put("xin_female", female);
            }
            male_total += male;
            female_total += female;
        }
        map.put("male_total", male_total);
        map.put("female_total", female_total);
        map.put("female_rate", fmt.format(female_total / regular_total1));

        //用工形式
        Long regular_all = 0L, other_all = 0L, intern_all = 0L, rehire_all = 0L, outsource_all = 0L;
        for(String str : company){
            list = pmEmployeeMapper.countByEmployeeType(endDate, str);
            Long regular = 0L, other = 0L, intern = 0L, rehire = 0L, outsource = 0L;
            for(HashMap<String, Long> item : list){
                String type = String.valueOf(item.get("employment_type"));
                if (type.contains("正式员工")){
                    regular += item.get("count");
                } else if (type.contains("实习")){
                    intern += item.get("count");
                }else if (type.contains("退休")){
                    rehire += item.get("count");
                }else if (type.contains("劳务") || type.contains("外包") ){
                    outsource += item.get("count");
                } else {
                    other += item.get("count");
                }
            }
            Long hotel_all = regular + other + intern + rehire + outsource;
            if (str.equals("宁波市会展博览集团有限公司")){
                map.put("hz_regular", regular);
                map.put("hz_other", other);
                map.put("hz_intern", intern);
                map.put("hz_rehire", rehire);
                map.put("hz_outsource", outsource);
                map.put("hz_all", hotel_all);
            } else if (str.equals("宁波国际会议中心管理有限公司")){
                map.put("gh_regular", regular);
                map.put("gh_other", other);
                map.put("gh_intern", intern);
                map.put("gh_rehire", rehire);
                map.put("gh_outsource", outsource);
                map.put("gh_all", hotel_all);
            } else if (str.equals("宁波国际会议中心酒店有限公司")){
                map.put("ghh_regular", regular);
                map.put("ghh_other", other);
                map.put("ghh_intern", intern);
                map.put("ghh_rehire", rehire);
                map.put("ghh_outsource", outsource);
                map.put("ghh_all", hotel_all);
            } else if (str.equals("宁波钱湖酒店有限公司东钱湖康得思酒店")){
                map.put("kd_regular", regular);
                map.put("kd_other", other);
                map.put("kd_intern", intern);
                map.put("kd_rehire", rehire);
                map.put("kd_outsource", outsource);
                map.put("kd_all", hotel_all);
            } else if (str.equals("宁波文化广场朗豪酒店有限公司")){
                map.put("lh_regular", regular);
                map.put("lh_other", other);
                map.put("lh_intern", intern);
                map.put("lh_rehire", rehire);
                map.put("lh_outsource", outsource);
                map.put("lh_all", hotel_all);
            } else if (str.equals("宁波钱湖国宾馆管理有限公司")){
                map.put("qian_regular", regular);
                map.put("qian_other", other);
                map.put("qian_intern", intern);
                map.put("qian_rehire", rehire);
                map.put("qian_outsource", outsource);
                map.put("qian_all", hotel_all);
            } else {
                map.put("xin_regular", regular);
                map.put("xin_other", other);
                map.put("xin_intern", intern);
                map.put("xin_rehire", rehire);
                map.put("xin_outsource", outsource);
                map.put("xin_all", hotel_all);
            }
            regular_all += regular;
            other_all += other;
            intern_all += intern;
            rehire_all += rehire;
            outsource_all += outsource;
        }
        map.put("regular_all", regular_all);
        map.put("other_all", other_all);
        map.put("intern_all", intern_all);
        map.put("rehire_all", rehire_all);
        map.put("outsource_all", outsource_all);

        return map;
    }

    //报送表
    @Override
    public Map<String, Object> getMap5(String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        String[] company = {"宁波市会展博览集团有限公司", "宁波国际会议中心管理有限公司", "宁波国际会议中心酒店有限公司", "宁波钱湖酒店有限公司东钱湖康得思酒店", "宁波文化广场朗豪酒店有限公司", "宁波钱湖国宾馆管理有限公司", "宁波新芝宾馆经营有限公司"};
        List<HashMap<String, Long>> list;
        LambdaQueryWrapper<PmEmployee> wrapper = Wrappers.lambdaQuery();
        wrapper.le(PmEmployee::getOnboardingTime, endDate)
                .eq(PmEmployee::getIsDeleted, false)
                .eq(PmEmployee::getIsLeave, false);
        int all = pmEmployeeMapper.selectCount(wrapper);
        int regular_total = pmEmployeeMapper.selectCount(wrapper.eq(PmEmployee::getIsRegular, true));
        map.put("all", all);
        map.put("total", regular_total);

        //百分数转化
        NumberFormat fmt = NumberFormat.getPercentInstance();
        fmt.setMaximumFractionDigits(2);
        double regular_total1 = regular_total;

        //年龄统计
        Long under30_total = 0L, under50_total = 0L, above51_total = 0L;
        for(String str : company){
            list = pmEmployeeMapper.countByAge(endDate, str);
            Long under30 = 0L, under50 = 0L, above51 = 0L;
            for(HashMap<String, Long> item : list){
                String type = String.valueOf(item.get("age_temp"));
                if (type.contains("30-")){
                    under30 += item.get("count");
                } else if (type.contains("31-40") || type.contains("41-50")){
                    under50 += item.get("count");
                } else if (type.contains("51+")){
                    above51 += item.get("count");
                }
            }
            Long total = under30 + under50 + above51;
            double total1 = total;
            if (str.equals("宁波市会展博览集团有限公司")){
                map.put("hz_under30", under30);
                map.put("hz_under50", under50);
                map.put("hz_above51", above51);
                if (total == 0){
                    map.put("hz_under30_rate", fmt.format(0));
                    map.put("hz_under50_rate", fmt.format(0));
                    map.put("hz_above51_rate", fmt.format(0));
                }else {
                    map.put("hz_under30_rate", fmt.format(under30 / total1));
                    map.put("hz_under50_rate", fmt.format(under50 / total1));
                    map.put("hz_above51_rate", fmt.format(above51 / total1));
                }
            } else if (str.equals("宁波国际会议中心管理有限公司")){
                map.put("gh_under30", under30);
                map.put("gh_under50", under50);
                map.put("gh_above51", above51);
                if (total == 0){
                    map.put("gh_under30_rate", fmt.format(0));
                    map.put("gh_under50_rate", fmt.format(0));
                    map.put("gh_above51_rate", fmt.format(0));
                }else {
                    map.put("gh_under30_rate", fmt.format(under30 / total1));
                    map.put("gh_under50_rate", fmt.format(under50 / total1));
                    map.put("gh_above51_rate", fmt.format(above51 / total1));
                }
            } else if (str.equals("宁波国际会议中心酒店有限公司")){
                map.put("ghh_under30", under30);
                map.put("ghh_under50", under50);
                map.put("ghh_above51", above51);
                if (total == 0){
                    map.put("ghh_under30_rate", fmt.format(0));
                    map.put("ghh_under50_rate", fmt.format(0));
                    map.put("ghh_above51_rate", fmt.format(0));
                }else {
                    map.put("ghh_under30_rate", fmt.format(under30 / total1));
                    map.put("ghh_under50_rate", fmt.format(under50 / total1));
                    map.put("ghh_above51_rate", fmt.format(above51 / total1));
                }
            } else if (str.equals("宁波钱湖酒店有限公司东钱湖康得思酒店")){
                map.put("kd_under30", under30);
                map.put("kd_under50", under50);
                map.put("kd_above51", above51);
                if (total == 0){
                    map.put("kd_under30_rate", fmt.format(0));
                    map.put("kd_under50_rate", fmt.format(0));
                    map.put("kd_above51_rate", fmt.format(0));
                }else {
                    map.put("kd_under30_rate", fmt.format(under30 / total1));
                    map.put("kd_under50_rate", fmt.format(under50 / total1));
                    map.put("kd_above51_rate", fmt.format(above51 / total1));
                }
            } else if (str.equals("宁波文化广场朗豪酒店有限公司")){
                map.put("lh_under30", under30);
                map.put("lh_under50", under50);
                map.put("lh_above51", above51);
                if (total == 0){
                    map.put("lh_under30_rate", fmt.format(0));
                    map.put("lh_under50_rate", fmt.format(0));
                    map.put("lh_above51_rate", fmt.format(0));
                }else {
                    map.put("lh_under30_rate", fmt.format(under30 / total1));
                    map.put("lh_under50_rate", fmt.format(under50 / total1));
                    map.put("lh_above51_rate", fmt.format(above51 / total1));
                }
            } else if (str.equals("宁波钱湖国宾馆管理有限公司")){
                map.put("qian_under30", under30);
                map.put("qian_under50", under50);
                map.put("qian_above51", above51);
                if (total == 0){
                    map.put("qian_under30_rate", fmt.format(0));
                    map.put("qian_under50_rate", fmt.format(0));
                    map.put("qian_above51_rate", fmt.format(0));
                }else {
                    map.put("qian_under30_rate", fmt.format(under30 / total1));
                    map.put("qian_under50_rate", fmt.format(under50 / total1));
                    map.put("qian_above51_rate", fmt.format(above51 / total1));
                }
            } else {
                map.put("xin_under30", under30);
                map.put("xin_under50", under50);
                map.put("xin_above51", above51);
                if (total == 0){
                    map.put("xin_under30_rate", fmt.format(0));
                    map.put("xin_under50_rate", fmt.format(0));
                    map.put("xin_above51_rate", fmt.format(0));
                }else {
                    map.put("xin_under30_rate", fmt.format(under30 / total1));
                    map.put("xin_under50_rate", fmt.format(under50 / total1));
                    map.put("xin_above51_rate", fmt.format(above51 / total1));
                }
            }
            under30_total += under30;
            under50_total += under50;
            above51_total += above51;
        }
        map.put("under30_rate", fmt.format(under30_total / regular_total1));
        map.put("under50_rate", fmt.format(under50_total / regular_total1));
        map.put("above51_rate", fmt.format(above51_total / regular_total1));

        //用工形式
        for(String str : company){
            list = pmEmployeeMapper.countByEmployeeType(endDate, str);
            Long regular = 0L, other = 0L, intern = 0L, rehire = 0L, outsource = 0L;
            for(HashMap<String, Long> item : list){
                String type = String.valueOf(item.get("employment_type"));
                if (type.contains("正式员工")){
                    regular += item.get("count");
                } else if (type.contains("实习")){
                    intern += item.get("count");
                } else if (type.contains("退休")){
                    rehire += item.get("count");
                } else if (type.contains("劳务")){
                    outsource += item.get("count");
                } else {
                    other += item.get("count");
                }
            }
//            Long hotel_all = regular + other + intern + rehire;
            if (str.equals("宁波市会展博览集团有限公司")){
                map.put("hz_other", other + outsource);
                map.put("hz_intern", intern);
                map.put("hz_rehire", rehire);
            } else if (str.equals("宁波国际会议中心管理有限公司")){
                map.put("gh_other", other + outsource);
                map.put("gh_intern", intern);
                map.put("gh_rehire", rehire);
            } else if (str.equals("宁波国际会议中心酒店有限公司")){
                map.put("ghh_other", other + outsource);
                map.put("ghh_intern", intern);
                map.put("ghh_rehire", rehire);
            } else if (str.equals("宁波钱湖酒店有限公司东钱湖康得思酒店")){
                map.put("kd_other", other + outsource);
                map.put("kd_intern", intern);
                map.put("kd_rehire", rehire);
            } else if (str.equals("宁波文化广场朗豪酒店有限公司")){
                map.put("lh_other", other + outsource);
                map.put("lh_intern", intern);
                map.put("lh_rehire", rehire);
            } else if (str.equals("宁波钱湖国宾馆管理有限公司")){
                map.put("qian_other", other + outsource);
                map.put("qian_intern", intern);
                map.put("qian_rehire", rehire);
            } else {
                map.put("xin_other", other + outsource);
                map.put("xin_intern", intern);
                map.put("xin_rehire", rehire);
            }
        }
        LambdaQueryWrapper<PmEmployee> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.like(PmEmployee::getCompany, "宁波市会展博览集团有限公司")
                .eq(PmEmployee::getIsRegular, true)
                .eq(PmEmployee::getIsDeleted, false)
                .le(PmEmployee::getOnboardingTime, endDate)
                .eq(PmEmployee::getIsLeave, false);
        int hz = pmEmployeeMapper.selectCount(wrapper1);
        LambdaQueryWrapper<PmEmployee> wrapper2 = Wrappers.lambdaQuery();
        wrapper2.like(PmEmployee::getCompany, "宁波国际会议中心管理有限公司")
                .eq(PmEmployee::getIsDeleted, false)
                .eq(PmEmployee::getIsRegular, true)
                .le(PmEmployee::getOnboardingTime, endDate)
                .eq(PmEmployee::getIsLeave, false);
        int gh = pmEmployeeMapper.selectCount(wrapper2);
        map.put("hzgh", hz + gh);

        //骨干离职率计算
        Long leave_all = 0L;
        for(String str : company){
            Map<String, Long> map1 = pmEmployeeMapper.leaveManager(startDate, endDate, str);
            Long mount = map1.get("count");
            if (str.equals("宁波市会展博览集团有限公司")){
                map.put("hz_leave", mount);
            } else if (str.equals("宁波国际会议中心管理有限公司")){
                map.put("gh_leave", mount);
            } else if (str.equals("宁波国际会议中心酒店有限公司")){
                map.put("ghh_leave", mount);
            } else if (str.equals("宁波钱湖酒店有限公司东钱湖康得思酒店")){
                map.put("kd_leave", mount);
            } else if (str.equals("宁波文化广场朗豪酒店有限公司")){
                map.put("lh_leave", mount);
            } else if (str.equals("宁波钱湖国宾馆管理有限公司")){
                map.put("qian_leave", mount);
            } else {
                map.put("xin_leave", mount);
            }
            leave_all += mount;
        }
        double leave_rate = (double) leave_all / regular_total;
        map.put("leave_rate", fmt.format(leave_rate));
        map.put("leave_all", leave_all);

        return map;
    }

    //管理人员名单
    @Override
    public Map<String, Object> getMap6(String startDate, String endDate, String company) {

        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<PmEmployee> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(PmEmployee::getOnboardingTime, endDate).eq(PmEmployee::getIsLeave, false).eq(PmEmployee::getIsDeleted, false);
        wrapper.in(PmEmployee::getPositionStatus, new String[]{"中层正职", "中层副职", "经营层正职", "经营层副职"});
        if (company.equals("%")) wrapper.orderByDesc(PmEmployee::getCompany);
        else wrapper.eq(PmEmployee::getCompany, company);
        List<PmEmployee> list = pmEmployeeMapper.selectList(wrapper);
        map.put("list", list);
        map.put("start_date", startDate);
        map.put("end_date", endDate);
        return map;
    }

    //管理人员名单
    @Override
    public Map<String, Object> getMap7(String startDate, String endDate, String company) {

        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<PmEmployee> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(PmEmployee::getOnboardingTime, endDate).eq(PmEmployee::getIsLeave, false).eq(PmEmployee::getIsDeleted, false);
        if (company.equals("%")) wrapper.orderByDesc(PmEmployee::getCompany);
        else wrapper.eq(PmEmployee::getCompany, company);
        List<PmEmployee> list = pmEmployeeMapper.selectList(wrapper);
        for (int i=0; i < list.size(); i++){
            list.get(i).setId((long) i + 1);
        }
        map.put("list", list);
        map.put("start_date", startDate);
        map.put("end_date", endDate);
        return map;
    }
}